package com.wnhz.work.yz.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.wnhz.work.yz.BaseActivity;
import com.wnhz.work.yz.MyApplication;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.base.BaseRVAdapter;
import com.wnhz.work.yz.base.BaseViewHolder;
import com.wnhz.work.yz.bean.PersonBean;
import com.wnhz.work.yz.utils.Url;
import com.wnhz.work.yz.utils.xutils3.MyCallBack;
import com.wnhz.work.yz.utils.xutils3.XUtil;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class GroupInviteActivity extends BaseActivity implements View.OnClickListener {
    private EditText searchPerson;
    private RecyclerView has_person;
    private LinearLayout ll_easyicon;
    private ImageView recode;
    private TextView names;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_group_invite);
        initData();
        initTitle();
        initView();
    }

    private String group_id, name, img;

    private void initData() {
        group_id = getIntent().getStringExtra("groupId");
        name = getIntent().getStringExtra("name");
        img = getIntent().getStringExtra("img");
    }

    private void initView() {
        recode = (ImageView) findViewById(R.id.recode);
        names = (TextView) findViewById(R.id.team_name);
        searchPerson = (EditText) findViewById(R.id.search_person);
        searchPerson.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEND || (event != null && event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
                    upLoadSearch();
                    return true;
                }
                return false;
            }
        });
        has_person = (RecyclerView) findViewById(R.id.has_person);
        has_person.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        ll_easyicon = (LinearLayout) findViewById(R.id.ll_easyicon);
        if ("".equals(group_id) || group_id == null) {
            ll_easyicon.setVisibility(View.GONE);
        } else {
            ll_easyicon.setVisibility(View.VISIBLE);
            names.setText(name + "");
            try {
                x.image().bind(recode, img);
            } catch (Exception e) {
            }
        }

    }

    private PersonBean bean;
    private List<PersonBean.InfoEntity> listBean = new ArrayList<>();

    private void upLoadSearch() {
        Map<String, Object> map = new HashMap<>();
        map.put("token", MyApplication.getInstance().getUserBean().getToken());
        map.put("keywords", searchPerson.getText().toString());
        showDialog();
      /*  Set keys = map.keySet();
        if (keys != null) {
            Iterator iterator = keys.iterator();
            while (iterator.hasNext()) {
                Object key = iterator.next();
                Object value = map.get(key);
                Log.e("----" + key, ":" + value + "\n");
            }
        }*/
        XUtil.Post(Url.TEAM_SEARCHUSERWITHNOTTEAM, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.i("----", "onSuccess:= " + result);
                listBean.clear();
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    String info = jsonObject.optString("info");
                    if ("1".equals(res)) {
                        Gson gson = new Gson();
                        bean = gson.fromJson(result, PersonBean.class);
                        listBean = bean.getInfo();
                    } else {
                        if ("-1".equals(res)) {
                            info = "请重新登录";
                        }
                        MyToast(info);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                closeDialog();
                setData();
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                closeDialog();
            }
        });

    }

    private void setData() {
        Log.e("bbbb", listBean.size() + "==111");
        has_person.setAdapter(new BaseRVAdapter(this, listBean) {
            @Override
            public int getLayoutId(int viewType) {
                return R.layout.person_info;
            }

            @Override
            public void onBind(BaseViewHolder holder, final int position) {
                x.image().bind(holder.getImageView(R.id.head_img), listBean.get(position).getUser_img());
                holder.setTextView(R.id.head_name, listBean.get(position).getUser_name());
                holder.getView(R.id.max_ll).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = getIntent();
                        intent.putExtra("bean", listBean.get(position));
                        setResult(RESULT_OK, intent);
                        finish();
                    }
                });
            }

        });
    }

    private void initTitle() {
        TextView title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText("邀请加入");
//        title.setTextSize();
        title.setTextColor(getResources().getColor(R.color.main_color));
        findViewById(R.id.btn_main_left).setOnClickListener(this);
        //    findViewById(R.id.ll_easyicon).setOnClickListener(this);

        TextView submit = (TextView) findViewById(R.id.submit);
        submit.setText("搜索");
        submit.setVisibility(View.VISIBLE);
        submit.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_main_left:
                finish();
                break;
            case R.id.ll_easyicon:
                //   startActivity(new Intent(this, GroupQrCodeActivity.class));
                break;
            case R.id.submit:
                upLoadSearch();
                break;
        }
    }
}
